<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\html\controller;

//系统设计
class System extends Base
{
	public function _initialize()
    {
        parent::_initialize();
    }

    public function getHtmls() {

        $data = [];
        $dirs = glob(ROOT_PATH.'./template/html/*',GLOB_ONLYDIR);
        foreach ($dirs AS $dir)
        {
            $html = glob($dir.'/*.html');
            if($html) {
                foreach ($html AS $h) {
                    $controller = pathinfo($h)['filename'];
                    $module = [];
                    $module['url']  = '/html/'.basename($dir).'/'.$controller;
                    $comment = file($h)[0];
                    //注释规则<!--页面名称|act_id:动作名,act_id:动作名-->
//                    $comment = '<!--页面名称|act_id:动作名,act_id:动作名,act_id:动作名,-->';
                    preg_match('/\<\!\-\-(.+?)\-\-\>/',$comment,$match);
                    if($match) {
                        $rule = explode('|',$match[1]);
                        $module['title']  = $rule[0];
                        //动作
                        if( $rule[1] )
                        {
                            foreach (array_filter(explode(',',$rule[1])) AS $item) {
                                $item = explode(':',$item);
                                $module['action'][$item[0]] = $item[1];
                            }
                        }
                        //汇总
                        $data[basename($dir)][] = $module;
                    }
                }
            }
        }
//        //首次自动导入
////        halt($data);
//        $i = 0;
//        foreach ($data AS $p => $a) {

//            //父
//            $pid = \think\Db::name('menu')->insertGetId(['name'=>trim($p),'url'=>'###','sort' => $i]);
//            foreach ($a AS $s=>$m) {
//                $ppid = \think\Db::name('menu')->insertGetId(['name'=>trim($m['title']),'pid'=>$pid,'url'=>$m['url'],'sort' => $s]);
//                //action
//                if($action = $m['action']) {
//                    $k = 0;
//                    foreach ($action AS $act=>$title) {
//                        \think\Db::name('menu')->insertGetId(['name'=>trim($title),'pid'=>$ppid,'url'=>$m['url'],'action'=>$act,'sort' => $k,'mode'=>2]);
//                        $k++;
//                    }
//                }
//            }
//            $i++;

//        }


        return json($data);

    }

}
